
* REPLICATION CODE: JENNINGS & WLEZIEN, 'THE COMPARATIVE TIMELINE OF ELECTIONS', AMERICAN JOURNAL OF POLITICAL SCIENCE *

* FIGURE S2. Regression Coefficients, 900 days

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - ALL ELECTIONS - VOTE = POLLS *****************************
*********************************************************************************************

use LONG_MI.dta, clear

gen betas=.
gen cons=.
gen se_betas=.
gen se_cons=.

foreach x of numlist 1/900 {
quietly mi estimate, cmdok: mybstrpbetas vote_ poll_ if daysbeforeED==`x' 
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace betas=bt in `x'
scalar bt = bts[1,2]
replace cons=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_betas=sqrt(bt) in `x'
scalar bt = bts[2,2]
replace se_cons=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep betas se_betas cons se_cons
gen daysbeforeED=_n

drop if betas == .
mi xtset, clear 
sort daysbeforeED

gen betas_min95=betas+se_betas
gen betas_max95=betas-se_betas

gen cons_min95=cons+se_cons
gen cons_max95=cons-se_cons

save MI_ALL_BETAS.dta, replace

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - ALL ELECTIONS - VOTE = POLLS + PARTY *********************
*********************************************************************************************

use LONG_MI.dta, clear

drop if daysbeforeED>900
gen _parXcou_ = countryid+(partyid/100)

gen betas=.
gen cons=.
gen se_betas=.
gen se_cons=.

foreach x of numlist 1/900 {
quietly mi estimate, cmdok: mybstrpbetasareg vote_ poll_ if daysbeforeED==`x'
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace betas=bt in `x'
scalar bt = bts[1,2]
replace cons=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_betas=sqrt(bt) in `x'
scalar bt = bts[2,2]
replace se_cons=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep betas se_betas cons se_cons
gen daysbeforeED=_n

drop if betas == .
mi xtset, clear 
sort daysbeforeED

gen betas_min95=betas+se_betas
gen betas_max95=betas-se_betas

gen cons_min95=cons+se_cons
gen cons_max95=cons-se_cons

save MI_P_ALL_BETAS.dta, replace

****** SUPPLEMENTARY FIGURE S3.2 **************************************************************
*Coefficients from Predicting the Party Vote Share from the Poll Share, By Date in Election Cycle�Pooling all Elections

use MI_ALL_BETAS.dta, clear 
rename betas all_betas
rename betas_min95 all_betas_min95
rename betas_max95 all_betas_max95
drop se_betas
sort daysbeforeED
save MI_ALL_BETAS.dta, replace

use MI_P_ALL_BETAS.dta, clear
rename betas p_all_betas
rename betas_min95 p_all_betas_min95
rename betas_max95 p_all_betas_max95
drop se_betas
sort daysbeforeED
save MI_P_ALL_BETAS.dta, replace

merge daysbeforeED using MI_ALL_BETAS.dta

* 
twoway /*
*/  || line all_betas daysbeforeED if daysbeforeED<900, /*
*/  clpattern(solid) clcolor(black) clwidth(medthick)  /* 
*/  || line p_all_betas daysbeforeED if daysbeforeED<900, /*
*/  clpattern(dash) clcolor(black) clwidth(medthick)  /* 
*/  || line all_betas_min95 daysbeforeED if daysbeforeED<900,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  || line all_betas_max95 daysbeforeED if daysbeforeED<900,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  || line p_all_betas_min95 daysbeforeED if daysbeforeED<900,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  || line p_all_betas_max95 daysbeforeED if daysbeforeED<900,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  , scheme(s2mono) graphregion(color(white)) /*
*/  xtitle("Days Until Election") /*
*/  title("") /*
*/  ylabel(0(.1)1, gmax angle(horizontal)) /*
*/  ytick(0(.1)1) /*
*/  ytitle(Beta) /*
*/  xlabel(0(100)900) /*
*/  xscale(reverse) xscale(titlegap(2)) /*
*/  legend(order(1 "Without Party Controls" 2 "With Party Controls") /*
*/  rows(1) /*
*/  size(medsmall))

graph export FigS2.tif


